﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StuManagementSystem
{
    public partial class frm_Result : Form
    {
        DbContext db = new DbContext();
        private int StuCode;
        private string sql1;
        public frm_Result()
        {
            InitializeComponent();
        }
        public frm_Result(int Code)
        {
            InitializeComponent();
            this.StuCode = Code;
            sql1 = string.Format(@"SELECT StuSubjectName,StuMark,StuExamTime FROM dbo.StuSubject,dbo.StuMark WHERE StuMark.StuSubjectId = StuSubject.StuSubjectId AND StuCode = {0}",Code);
        }

        #region 窗体预加载
        /// <summary>
        /// 窗体预加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void frm_Result_Load(object sender, EventArgs e)
        {
            //不允许自动创建列
            this.dgv_StuMark.AutoGenerateColumns = false;
            //DataGridView整行选中
            this.dgv_StuMark.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //DataGridView不允许出现空行
            this.dgv_StuMark.AllowUserToAddRows = false;
            //不允许多选
            this.dgv_StuMark.MultiSelect = false;

            #region 加载科目数据
            var sql = string.Format(@"
                    SELECT -1 StuSubjectId,'-请选择-' StuSubjectName
                    union
                    SELECT StuSubjectId,StuSubjectName FROM dbo.StuSubject,StuGrade,dbo.StuInfo where StuGrade.StuGradeId = StuSubject.StuGradeId AND StuInfo.StuGradeId = StuGrade.StuGradeId AND dbo.StuInfo.StuCode ={0}", StuCode);
            DataSet ds = db.ExecDataSet(sql);
            var dt = ds.Tables[0];
            this.cbo_StuSubject.DisplayMember = "StuSubjectName";
            this.cbo_StuSubject.ValueMember = "StuSubjectId";
            this.cbo_StuSubject.DataSource = dt;
            //加载学生成绩信息
            FillWindow(this.sql1);
            #endregion
        }
        #endregion
        

        #region 填充成绩窗口
        /// <summary>
        /// 填充成绩窗口
        /// </summary>
        private void FillWindow(string sql)
        {
            DataSet ds = db.ExecDataSet(sql);
            this.dgv_StuMark.DataSource = ds.Tables[0];
        }
        #endregion

        #region 查询按钮
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Select_Click(object sender, EventArgs e)
        {
            var sql = sql1;
            if (!(string.IsNullOrWhiteSpace(this.cbo_StuSubject.Text) || this.cbo_StuSubject.Text.Equals("-请选择-")))
            {
                sql = string.Format(sql + " AND StuMark.StuSubjectId = {0}", Convert.ToString(this.cbo_StuSubject.SelectedValue));
            }
            if (!string.IsNullOrWhiteSpace(this.txt_StuMark.Text))
            {
                if (this.txt_StuMark.Text.Equals("及格"))
                    sql = string.Format(sql + " AND StuMark.StuMark >= 60 ");
                if (this.txt_StuMark.Text.Equals("未及格"))
                    sql = string.Format(sql + " AND StuMark.StuMark < 60 ");
            }
            FillWindow(sql);
            
        }
        #endregion




        #region 返回按钮
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Return_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

    }
}
